package com.cj.common.config.handerconfig;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Author cheng jun
 * @Description: 注册拦截器
 * @Date 2021/8/7 15:51
 */
@Configuration
public class HanderConfig implements WebMvcConfigurer {

    /**
     * 将自定义拦截器注册到spring bean中
     * @return
     */
    @Bean
    public GatewayInterceptor GatewayInterceptor(){
        return new GatewayInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册TestInterceptor拦截器
        InterceptorRegistration registration = registry.addInterceptor(GatewayInterceptor());
        // 所有路径都被拦截
        registration.addPathPatterns("/**");
        // 添加不拦截路径（登录、html静态资源、js静态资源、css静态资源）
        registration.excludePathPatterns("你的登陆路径", "/**/*.html", "/**/*.js", "/**/*.css", "/**/*.woff", "/**/*.ttf");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 匹配到resourceHandler,将URL映射至location,也就是本地文件夹,这里最后一个/不能不写
        String relativeFilePath = "/image";
        String windowsFilePath = "/usr/file/";
        registry.addResourceHandler(relativeFilePath + "/**").addResourceLocations("file:///" + windowsFilePath);
    }
}
